<template>
  <div class="permission-container">
    <div class="app-container">
      <!-- 表格 -->
      <el-card>
        <div style="text-align: right; margin-bottom: 20px">
          <el-button type="primary" size="small" @click="add(1, '0')"
            >添加权限</el-button
          >
        </div>

        <el-table border :data="permissionList" row-key="id">
          <el-table-column label="名称" prop="name" />
          <el-table-column label="标识" prop="code" />
          <el-table-column label="描述" prop="description" />
          <el-table-column label="操作">
            <template v-slot="scoped">
              <el-button
                type="text"
                @click="add(2, scoped.row.id)"
                v-if="scoped.row.type == '1'"
                >添加</el-button
              >
              <el-button type="text" @click="editfn(scoped.row.id)"
                >编辑</el-button
              >
              <el-button type="text" @click="del(scoped.row.id)">删除</el-button>
            </template>
          </el-table-column>
        </el-table>
      </el-card>

      <perDialog ref="dialog" @addperev="addperevfn" :iseait="iseait" :listlist='listlist'/>
    </div>
  </div>
</template>

<script>
import { transTree } from "../../utils/index";
import {
  getPermissionListAPI,
  addPermissionAPI,
  getPermissionDetailAPI,
  updatePermissionAPI,
  delPermissionAPI
} from "../../api/permission";
import perDialog from "./components/perDialog.vue";
export default {
  data() {
    return {
      permissionList: [],
      iseait: "",
      listlist:[]
    };
  },
  components: { perDialog },
  created() {
    this.getPermissionListAPIfn();
  },
  methods: {
    //自己网上找到树形结构方法
    toTree(data) {
      let result = [];
      if (!Array.isArray(data)) {
        return result;
      }
      data.forEach((item) => {
        delete item.children;
      });
      let map = {};
      data.forEach((item) => {
        map[item.id] = item;
      });
      data.forEach((item) => {
        let parent = map[item.pid];
        if (parent) {
          (parent.children || (parent.children = [])).push(item);
        } else {
          result.push(item);
        }
      });
      return result;
    },
    //获取界面数据
    async getPermissionListAPIfn() {
      const res = await getPermissionListAPI();
      //扁平化数据给子传过去，做校验使用
      this.listlist=res.data.data
      this.permissionList = transTree(res.data.data, "0");
      
      // console.log( this.permissionList);
    },
    //点击添加按钮

    add(type, pid) {
      // console.log(id);
      this.iseait = true;
      this.$refs.dialog.showDialog = true;
      this.$refs.dialog.formData.pid = pid;
      this.$refs.dialog.formData.type = type;
      this.$refs.dialog.disbleds=false
    },
    //编辑按钮
    async editfn(id) {
      this.iseait = false;
      this.$refs.dialog.showDialog = true;
      const res = await getPermissionDetailAPI(id);
      //  console.log(res);
      this.$refs.dialog.formData = res.data.data;
      this.$refs.dialog.disbleds=true
    },
    async del(id){
        const res=await delPermissionAPI(id)
        this.getPermissionListAPIfn();
    },
    //子组件中确定的按钮事件
    async addperevfn(formData) {
      if (this.iseait == false) {
        //编辑
        const res = await updatePermissionAPI(formData);
        // console.log(res);
      } else {
        //添加
        const res = await addPermissionAPI(formData);
        // console.log(res);
      }
      this.getPermissionListAPIfn();
    },
  },
};
</script>

<style lang="scss" scoped></style>